--[[

	By Slob187.
	DFrame by Garry Newman.
	
  ]]	
	

PANEL = {}

AccessorFunc( PANEL, "m_bDraggable", 		"Draggable", 		FORCE_BOOL )
AccessorFunc( PANEL, "m_bSizable", 			"Sizable", 			FORCE_BOOL )
AccessorFunc( PANEL, "m_bScreenLock", 		"ScreenLock", 		FORCE_BOOL )
AccessorFunc( PANEL, "m_bDeleteOnClose", 	"DeleteOnClose", 	FORCE_BOOL )

AccessorFunc( PANEL, "m_bBackgroundBlur", 	"BackgroundBlur", 	FORCE_BOOL )


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:Init()


	self:SetFocusTopLevel( true )

//	self:SetCursor( "sizeall" )
	
	self.ButtonPos = 115	
	
	self.btnClose = vgui.Create( "DSysButton", self )
	self.btnClose:SetType( "close" )
	self.btnClose.DoClick = function ( button ) self:Close() end
	self.btnClose:SetDrawBorder( false )
	self.btnClose:SetDrawBackground( false )
	
	self.lblTitle = vgui.Create( "DLabel", self )
	
	self:SetDraggable( true )
	self:SetSizable( false )
	self:SetScreenLock( true )
	self:SetDeleteOnClose( true )
	self:SetTitle( "#Untitled DFrame" )
	
	// This turns off the engine drawing
	self:SetPaintBackgroundEnabled( false )
	self:SetPaintBorderEnabled( false )
	
	self:SetSize( 700, 700 )
	
	self.m_fCreateTime = SysTime()

end

/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:ShowCloseButton( bShow )

	self.btnClose:SetVisible( bShow )

end

/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:SetTitle( strTitle )

	self.lblTitle:SetText( strTitle )

end


function PANEL:DrawList()

	self.List = vgui.Create( "DPanelList", self )
	self.List:SetSize( self:GetWide() - 170, self:GetTall() - 80 )
	self.List:SetPos( 150, 40 )
	self.List:EnableVerticalScrollbar( true ) 
	
end


function PANEL:List_SetAutoSize( bool )

	if self.List != nil then
	
		self.List:SetAutoSize( bool )
		
	end	
	
end	


function PANEL:List_AddItem( item )

	if self.List != nil then
	
		self.List:AddItem( item )
		
	end	
	
end	


function PANEL:List_EnableHorizontal( bool )

	if self.List != nil then
	
		if tobool( bool ) == nil then

			self.List:EnableHorizontal( false )
			
			return
			
		end	
		
		self.List:EnableHorizontal( bool )
		
	end	
	
end


function PANEL:List_Spacing( number )

	if self.List != nil then
	
		if tonumber( number ) == nil then
		
			self.List:SetSpacing( 5 )
			
			return
			
		end	
		
		self.List:SetSpacing( number )
		
	end	
	
end


function PANEL:List_Padding( number )

	if self.List != nil then
	
		if tonumber( number ) == nil then
		
			self.List:SetPadding( 15 )
			
			return
			
		end	
		
		self.List:SetPadding( number )
		
	end	
	
end


function PANEL:AddCloseButton()

	self.CloseButton = vgui.Create( "DButton", self )
	self.CloseButton:SetSize( 120, 20 )
	self.CloseButton:SetPos( 10, 90 )
	self.CloseButton:SetText( "Close" )
	self.CloseButton.DoClick = function()
	
		self:Close()
		
	end
	
end	


function PANEL:AddButton( name, cmd )

	self.AddedButton = vgui.Create( "DButton", self )
	self.AddedButton:SetSize( 120, 20 )
	self.AddedButton:SetPos( 10, self.ButtonPos )
	self.AddedButton:SetText( name )
	self.AddedButton.DoClick = function()
	
		LocalPlayer():ConCommand( cmd )
		
	end
	
	self.ButtonPos = self.ButtonPos + 25
	
end	


function PANEL:AddFuncButton( name, func )

	self.AddedButton = vgui.Create( "DButton", self )
	self.AddedButton:SetSize( 120, 20 )
	self.AddedButton:SetPos( 10, self.ButtonPos )
	self.AddedButton:SetText( name )
	self.AddedButton.DoClick = function()
	
		func()
		
	end
	
	self.ButtonPos = self.ButtonPos + 25
	
end	
	
	
/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:Close()

	self:SetVisible( false )

	if ( self:GetDeleteOnClose() ) then
		self:Remove()
	end

end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:Center()

	self:InvalidateLayout( true )
	self:SetPos( ScrW()/2 - self:GetWide()/2, ScrH()/2 - self:GetTall()/2 )

end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:Think()

	if (!self.Dragging) then return end
	
	local x = gui.MouseX() - self.Dragging[1]
	local y = gui.MouseY() - self.Dragging[2]

	// Lock to screen bounds if screenlock is enabled
	if ( self:GetScreenLock() ) then
	
		x = math.Clamp( x, 0, ScrW() - self:GetWide() )
		y = math.Clamp( y, 0, ScrH() - self:GetTall() )
	
	end
	
	self:SetPos( x, y )
	
end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:Paint()

	derma.SkinHook( "Paint", "Frame", self )
	return true

end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:OnMousePressed()

	if ( !self:GetDraggable() ) then return end

	self.Dragging = { gui.MouseX() - self.x, gui.MouseY() - self.y }
	self:MouseCapture( true )

end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:OnMouseReleased()

	self.Dragging = nil
	self:MouseCapture( false )

end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:PerformLayout()

	derma.SkinHook( "Layout", "Frame", self )

end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:IsActive()

	if ( self:HasFocus() ) then return true end
	if ( vgui.FocusedHasParent( self ) ) then return true end
	
	return false

end


derma.DefineControl( "GMRPFrame", "GMRP Frame", PANEL, "EditablePanel" )